function globalCorr = global_corr_AVA(seisReal, sySeis, globalCorr, nbrIt, nbrSim, fileid, maxNbrSim, NullDataValue, rangeOfAngles)
%% COMPUTE GLOBAL CORRECTION AND UPDATES IF BETTER CORRELATION IS ACHIEVED
%
% [INPUT]
%   sy_cell [cell]                  - SYNTHETIC SEISMOGRAM
%   seis_real [cell]                - REAL SEISMIC 
%   global_corr [noSim * noIt, 1]   - GLOBAL CORRELATION FROM PREVIOUS SIMULATION
%   noIt                            - ITERATION NUMBER
%   noSim                           - SIMULATION NUMBER
%   fileid                          - LOG FILE
% 
% [OUTPUT]
%   global_corr                     - GLOBAL CORRELATION FROM PREVIOUS SIMULATION
% 
% MODIFICATIONS:
% JUL 2019 - Update from BF handles null data
% 13 JAN 2016 - FOR LOOPS ON cc_mean_grid REMOVED
% 07 JAN 2016 - GLOBAL CORRELATION COEFFICIENTS FOR ALL REALIZATIONS ARE
%               STORED IN global_corr [noSim * noIt, 1] VARIABLE.
% 02 OUT 2012 - CORRECTION IN BUILDING SEISMIC ARRAY. PERFORMANCE UPDATE
% 
% LA 11 SEP 2012
% 

%% COMPUTE GLOBAL CORRELATION AND TEST AGAINST PREVIOUS SIMULATION

%% SELECT RANGE OF ANGLES
seisReal    = cellfun(@(x) x(:,rangeOfAngles(1,1):rangeOfAngles(1,2):3), seisReal, 'UniformOutput', false);
sySeis      = cellfun(@(x) x(:,rangeOfAngles(1,1):rangeOfAngles(1,2):3), sySeis, 'UniformOutput', false);

%% Do correlation in a vector
seisReal    = cell2mat(seisReal);
sySeis      = cell2mat(sySeis);
seisReal    = seisReal(:);
sySeis      = sySeis(:);

temp        = corrcoef(seisReal(find(isnan(sySeis) ~= 1)), sySeis(find(isnan(sySeis) ~= 1)));

fprintf(fileid, '%s \n',[ 'Global Correlation - Iteration - ',num2str(nbrIt), ' - Simulation - ', num2str(nbrSim),' - ', num2str(temp(1,2))]); % WRITE NUMBER OF SIM


if(temp(1,2) >= max(globalCorr(:,1)))

    fprintf(fileid, '%s \n', ['NEW GLOBAL CORRELATION: - ITERATION - ', num2str(nbrIt), 'SIMULATION - ', num2str(nbrSim),'-', num2str(temp(1,2))]); % WRITE NUMBER OF SIM

end

globalCorr(((nbrIt - 1) * maxNbrSim) + nbrSim,1) = temp(1,2);

fprintf(fileid, '%s \n',[ 'Global Correlation:  - iteration - ',num2str(nbrIt), ' - Simulation - ', num2str(nbrSim),'-', num2str(temp(1,2))]); % WRITE NUMBER OF SIM

clear temp

end
